<template>
    <div>
        <div style="height: 50px">
            <el-input
                    placeholder="请输入名称"
                    name="username"
                    v-model:value="username"
                    suffix-icon="el-icon-user"
                    style="width: 200px;padding-right: 5px;"
            >
            </el-input>
            <el-input
                    placeholder="请输入邮箱"
                    name="email"
                    v-model:value="email"
                    suffix-icon="el-icon-message"
                    style="width: 200px;padding-right: 5px;"
            >
            </el-input>
            <el-input
                    placeholder="请输入地址"
                    name="address"
                    v-model:value="address"
                    suffix-icon="el-icon-map-location"
                    style="width: 200px"
            >
            </el-input>

            <el-button class="el_margin_lr" type="primary" icon="el-icon-search" @click="load" >搜索</el-button>

            <el-button class="el_margin_lr" type="warning" icon="el-icon-search" @click="reset" >重置</el-button>
        </div>

        <div style="height: 50px">
            <el-button type="primary" @click="toAddUser">新增<i class="el-icon-circle-plus-outline" style="margin-left: 2px"></i></el-button>
            <el-button type="danger" @click="batchDeleteUsers">批量删除<i class="el-icon-remove-outline" style="margin-left: 2px"></i></el-button>




            <el-upload
                    style="display: inline-flex; margin-left: 5px;margin-right: 5px;"
                    action="http://localhost:8081/user/importUsers"
                    :accept="'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'"
                    :show-file-list="false"
                    :on-success="importFiles">
                <el-button size="small" type="primary" style="height: 40px;width: 86px;">导入<i class="el-icon-bottom" style="margin-left: 2px"></i></el-button>
            </el-upload>




            <el-button type="primary" @click="exportFiles">导出<i class="el-icon-top" style="margin-left: 2px"></i></el-button>



        </div>

        <el-table :data="tableData" header-cell-class-name="headerTableBg" @selection-change="handleSelectionChange">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column prop="id" label="ID" width="100"></el-table-column>
            <el-table-column prop="username" label="用户名" width="140"></el-table-column>
            <el-table-column prop="nickname" label="昵称" width="120"></el-table-column>
            <el-table-column prop="phone" label="电话" width="200"></el-table-column>
            <el-table-column prop="email" label="邮箱" width="200"></el-table-column>
            <el-table-column prop="address" label="地址" width="150"></el-table-column>
            <el-table-column prop="roleFlag" label="角色" width="180"></el-table-column>
            <el-table-column prop="operation" label="操作">
                <template slot-scope="scope">


                    <el-button v-if="checkRole_Teacher(scope.row.roleFlag)" type="warning" @click="queryCourses(scope.row.id)">查看教授课程<i class="el-icon-notebook-2" style="margin-left: 2px"></i></el-button>
                    <el-button v-if="checkRole_Student(scope.row.roleFlag)" type="info" @click="querySelectCourses(scope.row.id)">查看选课信息<i class="el-icon-notebook-2" style="margin-left: 2px"></i></el-button>

                    <el-button type="success" @click="toEdit(scope.row)">编辑<i class="el-icon-edit" style="margin-left: 2px"></i></el-button>

                    <el-popconfirm
                            class="el_margin_lr"
                            confirm-button-text='确定'
                            cancel-button-text='取消'
                            icon="el-icon-info"
                            icon-color="red"
                            title="确认删除该用户吗？"
                            @confirm="deleteUser(scope.row.id)">
                        <el-button  type="danger" slot="reference">删除<i class="el-icon-remove-outline" style="margin-left: 2px"></i></el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>

        </el-table>
        <div >
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageIndex"
                    :page-sizes="[5, 10, 15, 20]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="totalNum">
            </el-pagination>
        </div>

        <el-dialog :title="title" style="text-align: center;" :visible.sync="dialogFormVisible" width="30%">
            <div v-if="title=='教授课程' || title=='选课信息'">
                <el-table
                        :data="courses"
                        style="width: 100%">
                    <el-table-column
                            prop="id"
                            label="id"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="name"
                            label="课程名"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="score"
                            label="学分">
                    </el-table-column>
                    <el-table-column
                            prop="time"
                            label="学时">
                    </el-table-column>
                </el-table>
                <div slot="footer" class="dialog-footer" style="margin-top: 10px;">
                    <el-button type="primary" @click="dialogFormVisible = false" style="width: 25%">确 定</el-button>
                </div>
            </div>
            <div v-else>
                <el-form label-width=80px >
                    <el-form-item label="用户名" >
                        <el-input v-model="form.username" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="昵称" >
                        <el-input v-model="form.nickname" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="电话" >
                        <el-input v-model="form.phone" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="邮箱" >
                        <el-input v-model="form.email" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="地址" >
                        <el-input v-model="form.address" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="角色" >
                        <template >
                            <el-select style="width: 100%" v-model="form.roleFlag" placeholder="请选择">
                                <el-option
                                        v-for="item in options"
                                        :key="item"
                                        :label="item"
                                        :value="item">
                                </el-option>
                            </el-select>
                        </template>
                    </el-form-item>

                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="addUser">确 定</el-button>
                </div>
            </div>

        </el-dialog>

    </div>

</template>

<script>
    import request from "@/utils/request";
    import {constants} from "@/assets/common/contants";

    export default {
        name: "Main",
        data() {
            return {
                tableData: null,
                totalNum: 0,
                pageSize: 10,
                pageIndex:1,
                username:"",
                address:"",
                email:"",
                dialogFormVisible:false,
                form: {},
                multipleSelection: [],
                title:"",
                options:[],
                courses:[]
            }
        },
        created() {
            this.load();
        },
        methods: {
            checkRole_Teacher(roleFlag){
                if(roleFlag===constants.ROLE_TEACHER){
                    return true;
                }
                else return false;
            },
            checkRole_Student(roleFlag){
                if(roleFlag===constants.ROLE_STUDENT){
                    return true;
                }
                else return false;
            },
            queryCourses(id){
                request.get("course/queryCourse",{
                    params:{
                        id: id,
                    }
                }).then(res=>{
                    if (res.code === '200'){
                        this.title='教授课程';
                        this.dialogFormVisible=true;
                        this.courses=res.data;
                    }
                })

            },
            querySelectCourses(userId){
                request.get("userCourse/queryUserCourseInfo",{
                    params:{
                        userId:userId
                    }
                }).then(res=>{
                    if(res.code==='200'){
                        this.title="选课信息";
                        this.dialogFormVisible=true;
                        this.courses=res.data;
                    }
                })
            },
            load(){
                // let url="http://localhost:8081/user/queryUser?pageIndex="+this.pageIndex
                //         +"&pageSize="+this.pageSize
                //         +"&username="+this.username
                //         +"&address="+this.address
                //         +"&email="+this.email;
                // fetch(url)
                //     .then(res => res.json())
                //     .then(res => {
                //         this.totalNum=res.totalNum;
                //         this.tableData=res.usersList;
                //     });
                request.get("user/queryUser",{
                    params: {
                        pageIndex: this.pageIndex,
                        pageSize: this.pageSize,
                        username: this.username,
                        address: this.address,
                        email: this.email
                    }
                }).then(res=>{
                    this.totalNum=res.totalNum;
                    this.tableData=res.usersList;
                })

            },
            reset(){
                this.username="";
                this.address="";
                this.email="";
                this.load();
            },
            toAddUser(){
                this.title="新增用户";
                this.dialogFormVisible=true;
                this.form={};
            },
            addUser(){
                request.post("user/saveUser",this.form).then(res =>{
                    if (res){
                        this.$message({
                            message: '修改用户成功',
                            type: 'success'
                        });
                        this.dialogFormVisible=false;
                        this.load();
                    }else{
                        this.$message.error("修改用户失败")
                    }
                })
            },handleSizeChange(pageSize){ //页面大小变化
                this.pageSize=pageSize;
                this.load();
            },
            handleCurrentChange(pageIndex){  //当前页变化
                this.pageIndex=pageIndex;
                this.load();
            },
            toEdit(row){
                this.form=row;
                this.title="修改用户";
                this.dialogFormVisible=true;
                request.get("role/queryAllRoleFlags").then(res=>{
                    if (res.code=='200'){
                        this.options=res.data;
                    }
                })
            },
            deleteUser(id){
                request.get("user/deleteUser/"+id).then(res =>{
                    if (res){
                        this.$message({
                            message: '删除用户成功',
                            type: 'success'
                        });
                        this.dialogFormVisible=false;
                        this.load();
                    }else{
                        this.$message.error("删除用户失败")
                    }
                })
            },
            handleSelectionChange(val){
                this.multipleSelection=val;
            },
            batchDeleteUsers(){
                let ids=this.multipleSelection.map(v =>v.id);
                request.post("user/batchDeleteUsers",ids).then(res => {
                    if (res>0){
                        this.$message({
                            message: '成功删除'+res+'条数据',
                            type: 'success'
                        });
                        this.dialogFormVisible=false;
                        this.load();
                    }else{
                        this.$message.error("批量删除用户失败")
                    }
                })
            },
            exportFiles(){
                window.open("http://localhost:8081/user/exportUsers");
                this.$message.success('导出成功');
            },
            importFiles(){
                this.$message.success('导入成功');
                this.load();
            },
        }
    }
</script>

<style scoped>


    /deep/ .el-upload-dragger {
        width: 86px;
        height: 40px;
        text-align: center;
        cursor: pointer;
        overflow: hidden;
    }
</style>
